#ifndef NPIPE_H
#define NPIPE_H

//
// This class uses Async Procedure Calls (APC) and relys upon the *caller*
// to eventually enter a wait state (MsgWaitFor..., Sleep, WaitFor..).
//
// Note that the APC will *only* get called when the caller is in a wait state.
// (If you never get called through recvd(), then you haven't entered a wait
// state).
//

class NamedPipe
{
	public:
		NamedPipe(int32 MaxBufSize);
		virtual ~NamedPipe();

		bool8 open(TCHAR* PipeName);
		void close();

		bool8 send(uint8* Msg, int32 Len);

		virtual void ioerror() { }
		//
		// recvd:  only whole messages are received (provided MaxBufSize is
		//		   large enough; otherwise, problem...ERROR_INSUFFICIENT_BUFFER
		//		   is not currently handled)
		//
		virtual void recvd(uint8* /*Msg*/, int32 /*Len*/) { }

	private:
		static VOID FAR PASCAL ReadDone(DWORD, DWORD, LPOVERLAPPED);

		bool8 postRead();
		void readDone(DWORD, DWORD, LPOVERLAPPED);

	private:
		uint8* buf;
		int32 bufSize;
		int32 connected;
		HANDLE hPipe;
		LPOVERLAPPED overRd, overWr;
};

#endif
